package models

import (
	"time"
)

// RiskControlRule 风控规则模型
type RiskControlRule struct {
	ID          int       `gorm:"primaryKey;size:50" json:"id"`
	Name        string    `gorm:"size:100;not null" json:"name"`
	Description string    `gorm:"size:500" json:"description"`
	Scene       string    `gorm:"size:50;not null;index" json:"scene"`
	Priority    string    `gorm:"size:20;not null" json:"priority"` // high, medium, low
	IsActive    bool      `gorm:"not null;default:true" json:"is_active"`
	Conditions  string    `gorm:"type:text;not null" json:"conditions"` // JSON格式存储条件列表
	Actions     string    `gorm:"type:text;not null" json:"actions"`    // JSON格式存储动作列表
	CreatedAt   time.Time `json:"created_at"`
	UpdatedAt   time.Time `json:"updated_at"`
	CreatedBy   string    `gorm:"size:50" json:"created_by"`
}

// TableName 指定表名
func (RiskControlRule) TableName() string {
	return "risk_control_rules"
}

// RuleCondition 规则条件模型（用于存储单个条件）
type RuleCondition struct {
	ID        uint      `gorm:"primaryKey" json:"id"`
	RuleID    string    `gorm:"size:50;not null;index" json:"rule_id"`
	Field     string    `gorm:"size:50;not null" json:"field"`
	Operator  string    `gorm:"size:20;not null" json:"operator"`
	Value     string    `gorm:"size:100;not null" json:"value"`
	Logical   string    `gorm:"size:10" json:"logical"` // AND, OR
	CreatedAt time.Time `json:"created_at"`
	UpdatedAt time.Time `json:"updated_at"`
}

// TableName 指定表名
func (RuleCondition) TableName() string {
	return "rule_conditions"
}

// RuleAction 规则动作模型（用于存储单个动作）
type RuleAction struct {
	ID        uint      `gorm:"primaryKey" json:"id"`
	RuleID    string    `gorm:"size:50;not null;index" json:"rule_id"`
	Type      string    `gorm:"size:50;not null" json:"type"` // verify, warning, limit, block
	Details   string    `gorm:"size:500" json:"details"`
	CreatedAt time.Time `json:"created_at"`
	UpdatedAt time.Time `json:"updated_at"`
}

// TableName 指定表名
func (RuleAction) TableName() string {
	return "rule_actions"
}

// RiskLog 风控日志模型
type RiskLog struct {
	ID                  uint      `gorm:"primaryKey" json:"id"`
	LogID               string    `gorm:"size:50;not null;uniqueIndex" json:"log_id"`
	RuleName            string    `gorm:"size:100;not null" json:"rule_name"`
	Scene               string    `gorm:"size:50;not null;index" json:"scene"`
	UserID              uint      `gorm:"index" json:"user_id"`
	Username            string    `gorm:"size:50" json:"username"`
	Triggered           bool      `gorm:"not null" json:"triggered"`
	ExecutionResult     string    `gorm:"size:50;not null" json:"execution_result"`
	TriggeredConditions string    `gorm:"size:500" json:"triggered_conditions"`
	ExecutedActions     string    `gorm:"size:500" json:"executed_actions"`
	IpAddress           string    `gorm:"size:50" json:"ip_address"`
	ExtraData           string    `gorm:"type:text" json:"extra_data"` // JSON格式存储额外数据
	CreatedAt           time.Time `json:"created_at"`
}

// TableName 指定表名
func (RiskLog) TableName() string {
	return "risk_logs"
}
